package com.fita.admin.entity.search.report;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.fita.entity.wallet.WithdrawRecordPaidStatus;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 11200
 * @date: 2019/6/18
 */
public class WithdrawRecordSearch implements QueryBuilder {

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date beginCreateTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date endCreateTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date beginPaidTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date endPaidTime;

    private List<Long> agents;

    private WithdrawRecordPaidStatus withdrawRecordPaidStatus = WithdrawRecordPaidStatus.UNPAID;

    private QueryFilter queryFilter;

    private static final QueryOrder queryOrder = new QueryOrder().add("id", "DESC");

    private static final String sentence = "FROM WithdrawRecord WHERE disabled = false";

    @Override
    public QueryFilter getPreparedFilter(boolean refresh) {
        if (queryFilter == null || refresh) {

            queryFilter = new QueryFilter();

            queryFilter.addSentence("operator.id IN (:agents)", "agents", agents);

            queryFilter.addEquals("paidStatus", withdrawRecordPaidStatus);

            queryFilter.addDateBetween("createTime", beginCreateTime, endCreateTime);

            queryFilter.addDateBetween("payTime", beginPaidTime, endPaidTime);
        }
        return queryFilter;
    }

    @Override
    public QueryOrder getOrder() {
        return queryOrder;
    }

    @Override
    public String buildSentenceTemplate() {

        return sentence + getPreparedFilter(false).clauseSentence()+ queryOrder.sentence();
    }

    @Override
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }

    public List<Long> getAgents() {
        return agents;
    }

    public void setAgents(List<Long> agents) {
        this.agents = agents;
    }

    public WithdrawRecordPaidStatus getWithdrawRecordPaidStatus() {
        return withdrawRecordPaidStatus;
    }

    public void setWithdrawRecordPaidStatus(WithdrawRecordPaidStatus withdrawRecordPaidStatus) {
        this.withdrawRecordPaidStatus = withdrawRecordPaidStatus;
    }

    public Date getBeginCreateTime() {
        return beginCreateTime;
    }

    public void setBeginCreateTime(Date beginCreateTime) {
        this.beginCreateTime = beginCreateTime;
    }

    public Date getEndCreateTime() {
        return endCreateTime;
    }

    public void setEndCreateTime(Date endCreateTime) {
        this.endCreateTime = endCreateTime;
    }

    public Date getBeginPaidTime() {
        return beginPaidTime;
    }

    public void setBeginPaidTime(Date beginPaidTime) {
        this.beginPaidTime = beginPaidTime;
    }

    public Date getEndPaidTime() {
        return endPaidTime;
    }

    public void setEndPaidTime(Date endPaidTime) {
        this.endPaidTime = endPaidTime;
    }
}
